package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.*;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@RestController
@RequestMapping
@CrossOrigin
@Slf4j
public class RoleController {
    @Reference(version = "1.0.0")
    private RoleService roleService;


    //分页查询
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @GetMapping(path = "/role/page")
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询检查组信息
        try {
            //分页查询
            PageResult pageResult = roleService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());

            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //修改数据
    @PreAuthorize("hasAnyAuthority('ROLE_EDIT')")
    @PutMapping(path = "/role")
    public Result updateRole(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.updateRole(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //添加数据
    @PreAuthorize("hasAnyAuthority('ROLE_ADD')")
    @PostMapping(path = "/role")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            log.info("前端添加角色数局role:{},permisssionIds:{},menuIds:{}",role.toString(),permissionIds,menuIds);
            roleService.addRole(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //根据id查询

    @GetMapping(path = "/role/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    //根据id删除数据
    @PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
    @DeleteMapping(path = "/role/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        log.info("deleteById开始执行");
        try {
            Boolean state = roleService.deleteById(id);
            if (state) {
                return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
            } else {
                return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }


    //根据id查询角色关联的菜单信息
    @GetMapping(path = "/role/{id}/menu")
    public Result roleMenu(@PathVariable("id") Integer id) {
        try {
            // /role/{id}/menu
            Integer[] roleMenu = roleService.roleMenu(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据id查询角色关联的权限信息
    @GetMapping(path = "/role/{id}/permission")
    public Result rolePermission(@PathVariable("id") Integer id) {
        try {
            // /role/{id}/permission
            Integer[] rolePermission = roleService.rolePermission(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, rolePermission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

}
